﻿using Business;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BVVD_Website.ql_thi_sinh
{
    public partial class frmKythiMonthi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvKM.Culture = new CultureInfo("vi-VN");
                grvKM.Rebind();
               
                BVVDDataContext db = new BVVDDataContext();
                List<LST_KYTHI> list = (from kt in db.LST_KYTHIs
                                        where kt.TRANGTHAI.Equals("1")
                                        select kt).ToList();
                ddlKYTHI.DataSource = list;
                ddlKYTHI.DataValueField = "MAKT";
                ddlKYTHI.DataTextField = "TENKT";
                ddlKYTHI.DataBind();

                fillgrvKT();
                grvKM.DataBind();
            }
        }

        protected void ddlKYTHI_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            fillgrvKT();
            grvKM.DataBind();
        }

        protected void grvKM_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillgrvKT();
        }

        protected void grvKM_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                BVVDDataContext db = new BVVDDataContext();
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;
                
                RadDropDownList ddlMAMH = editFormItem.FindControl("ddlMAMH") as RadDropDownList;

                Label lblMAKT = editFormItem.FindControl("lblMAKT") as Label;
                Label lblTENKT = editFormItem.FindControl("lblTENKT") as Label;
                List<LST_MONHOC> list = (from mh in db.LST_MONHOCs
                                         select mh).ToList();
                ddlMAMH.DataSource = list;
                ddlMAMH.DataValueField = "MAMH";
                ddlMAMH.DataTextField = "TENMH";
                ddlMAMH.DataBind();
                LinkButton btnUpdate = editFormItem.FindControl("btnUpdate") as LinkButton;

                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    if (ddlKYTHI.SelectedIndex >= 0)
                    {
                        lblMAKT.Text = ddlKYTHI.SelectedText;
                        //txtMAMH.Text = ddlMONHOC.SelectedValue;
                        lblTENKT.Text = ddlKYTHI.SelectedText;
                        //lblTENMH.Text = ddlMONHOC.SelectedText;
                        ddlMAMH.Enabled = true;
                    }
                    else {
                        lblMAKT.Text = "<a href='/ql-thi-sinh/frmKythi.aspx'>Quay lại để chọn kỳ thi</a";
                        ddlMAMH.Enabled = false;
                        btnUpdate.Enabled = false;
                        
                    }


                }
                else
                {

                    Label lblTRANGTHAI = editFormItem["TRANGTHAI"].FindControl("lblTRANGTHAI") as Label;
                    rblTRANGTHAI.SelectedValue = lblTRANGTHAI.Text.Equals("ACTIVE") ? "1" : "0";
                    ddlMAMH.SelectedValue = editFormItem["MAMH"].Text;
                    ddlMAMH.Enabled = false;                    

                }
            
            
            
            
            }
        }

        protected void grvKM_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            F_KYTHI_MONTHI km = (from dm in db.F_KYTHI_MONTHIs
                                   where dm.RECID.ToString().Equals(recid)
                                   select dm).SingleOrDefault();
            string delDM = "Xoá thành công môn học  <font color='red'>  " + km.LST_MONHOC.TENMH + "</font> trong danh sách các môn thi tại kỳ thi <font color='red'> " + km.LST_KYTHI.TENKT +"</font>" ;
            db.F_KYTHI_MONTHIs.DeleteOnSubmit(km);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = delDM;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }

        protected void grvKM_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;

            F_KYTHI_MONTHI km = new F_KYTHI_MONTHI
            {
                MAKT = ddlKYTHI.SelectedValue,
                MAMH = (insertedItem.FindControl("ddlMAMH") as RadDropDownList).SelectedValue,
                NGAYTHI =Convert.ToDateTime((insertedItem.FindControl("rdpNGAYTHI1") as RadDatePicker).DbSelectedDate),
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();

            db.F_KYTHI_MONTHIs.InsertOnSubmit(km);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới môn học <font color='red'> " + km.LST_MONHOC.TENMH + "</font> vào kỳ thi <font color='red'> " + km.LST_KYTHI.TENKT + "</font> thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n <br /> Nguyên nhân : " + ex.Message;

            }


            rnAlert.Show();
        }

        protected void grvKM_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            F_KYTHI_MONTHI km = (from item in db.F_KYTHI_MONTHIs
                                   where item.RECID.ToString().Equals(recid)
                                   select item).SingleOrDefault();
            //danhmuc.MALOAIDM = (editedItem.FindControl("ddlLOAIDM") as RadDropDownList).SelectedValue;
            km.MAMH = (editedItem.FindControl("ddlMAMH") as RadDropDownList).SelectedValue;
            km.NGAYTHI = Convert.ToDateTime((editedItem.FindControl("rdpNGAYTHI1") as RadDatePicker).DbSelectedDate);
            km.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            km.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công môn học   <font color='red'> " + km.LST_MONHOC.TENMH + "</font> trong kỳ thi <font color='red'> " + km.LST_KYTHI.TENKT + "</font>";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }

        private void fillgrvKT()
        {
            BVVDDataContext db = new BVVDDataContext();
            var list = (from kt in db.F_KYTHI_MONTHIs
                        select new
                        {
                            RECID = kt.RECID,
                            MAKT = kt.MAKT,
                            TENKT = kt.LST_KYTHI.TENKT,
                            MAMH = kt.MAMH,
                            TENMH = kt.LST_MONHOC.TENMH,
                            NGAYTHI = kt.NGAYTHI,
                            TRANGTHAI = kt.TRANGTHAI,
                            GHICHU = kt.GHICHU
                        }).ToList();
            if (ddlKYTHI.SelectedIndex >= 0)
            {
                grvKM.DataSource = list.Where(a => a.MAKT.ToString().Equals(ddlKYTHI.SelectedValue));
            }
            else
            {
                grvKM.DataSource = list;

            }

        }

        protected void grvKM_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //Add new" button clicked
                e.Canceled = true;
                //Prepare an IDictionary with the predefined values
                System.Collections.Specialized.ListDictionary newValues = new
                System.Collections.Specialized.ListDictionary();
                newValues["NGAYTHI"] = "";
                newValues["GHICHU"] = "";
                //Insert the item and rebind
                e.Item.OwnerTableView.InsertItem(newValues);
            }
        }
    }
}